package com.shiguo365.market.presenter.implement;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.view.IAddressAddView;
import com.shiguo365.market.display.view.IConfirmView;
import com.shiguo365.market.presenter.contract.IConfirmPresenter;

import java.util.List;

import biz.yfsoft.api.ae.api.AEClient;
import biz.yfsoft.api.ae.api.AEParam;
import biz.yfsoft.api.ae.api.BaseAECallback;

/**
 * Created by caomenglong on 16/8/12.
 */
public class ConfirmPresenter implements IConfirmPresenter {
    private static final String TAG = "ConfirmPresenter";
    private final Activity mActivity;
    private final IConfirmView mConfirmView;

    public ConfirmPresenter(@NonNull Activity activity, @NonNull IConfirmView testView) {
        this.mActivity = activity;
        this.mConfirmView = testView;
    }

    @Override
    public void getConfirmByUI(Integer uid) {
        AEClient _client = new AEClient("shiguo.order.confirmOrder");
        AEParam aeParam=new AEParam();
        aeParam.set("uid",uid);
        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(@NonNull JSONObject result) {
                Log.d(TAG,result.toJSONString());


                JSONArray jsonObjectList = result.getJSONArray("prolist");


                Log.d(TAG,jsonObjectList.toJSONString());

                mConfirmView.OnGetConfirmOK(jsonObjectList);
                mConfirmView.OnGetInfoOK(result);
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                mConfirmView.OnGetConfirmFinsh();
            }
        });

    }

    @Override
    public void addOrder(int uid,int aid,String finalAmount,String remark) {

        Log.d(TAG,"现在正在执行创建订单操作");
        Log.d(TAG,"uid："+uid);
        Log.d(TAG,"aid："+aid);
        Log.d(TAG,"finalAmount："+finalAmount);
        Log.d(TAG,"remark："+remark);

        AEClient _client = new AEClient("shiguo.order.createOrder");
        AEParam aeParam=new AEParam();
        aeParam.set("uid",uid);
        aeParam.set("aid",aid);
        aeParam.set("finalAmount",finalAmount);
        aeParam.set("remark",remark);
        _client.call(aeParam,new BaseAECallback() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(@NonNull JSONObject result) {
                //生成订单成功
                Log.d(TAG,"生成订单成功："+result.toString());
                mConfirmView.showConfirmMessages("恭喜您订单创建成功,系统正在为你服务");
                mConfirmView.confirmSuccess();
            }

            @Override
            public void onError( String errno, String message) {
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                Log.e(TAG,"onFinally ......." );
            }
        });

    }
}
